package com.googlecode.droidcare.common.impl.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;

import com.googlecode.droidcare.common.AppContext;
import com.googlecode.droidcare.common.api.net.HttpService;

public class ApacheHttpService implements HttpService<HttpUriRequest, HttpResponse> {
	
	private String navigationUrl;
	private DefaultHttpClient defaulthttpclient;
	private BasicHttpContext basichttpcontext;
	private ArrayList<NameValuePair> params;
	
	public ApacheHttpService() {
		defaulthttpclient = new DefaultHttpClient();
		defaulthttpclient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
		basichttpcontext = new BasicHttpContext();
		BasicCookieStore basiccookiestore = new BasicCookieStore();
		basichttpcontext.setAttribute("http.cookie-store", basiccookiestore);
		params = new ArrayList<NameValuePair>();
		
	}
	
	public HttpResponse get(HttpUriRequest request) {
		return post(request);
	}
	
	public HttpResponse post(HttpUriRequest request) {
		try {
			return defaulthttpclient.execute(request, basichttpcontext);
		} catch (ClientProtocolException e) {
			AppContext.getExceptionHandler().handle(e);
		} catch (IOException e) {
			AppContext.getExceptionHandler().handle(e);
		}
		return null;
	}
	
	@SuppressWarnings("deprecation")
	public Object post() {
		HttpPost httpPost = new HttpPost(navigationUrl);
		UrlEncodedFormEntity urlencodedformentity;
		HttpEntity httpEntity;
		try {
			urlencodedformentity = new UrlEncodedFormEntity(params, "UTF-8");
	        httpPost.setEntity(urlencodedformentity);
		} catch (UnsupportedEncodingException e) {
			AppContext.getExceptionHandler().handle(e);
		}
		try {
			httpEntity = post(httpPost).getEntity();
			httpEntity.consumeContent();
			return null;
		} catch (ClientProtocolException e) {
			AppContext.getExceptionHandler().handle(e);
		} catch (IOException e) {
			AppContext.getExceptionHandler().handle(e);
		}
		clearParams();
		return null;
	}
	
	public Object get() {
		HttpGet httpGet = new HttpGet(navigationUrl);
		HttpEntity httpEntity;
		try {
			httpEntity = get(httpGet).getEntity();
			BufferedReader rd = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
			return rd;
		} catch (ClientProtocolException e) {
			AppContext.getExceptionHandler().handle(e);
		} catch (IOException e) {
			AppContext.getExceptionHandler().handle(e);
		}
		return null;
	}
	
	public void clearParams() {
		params.clear();
	}
	
	public void addParam(String name, String value) {
		params.add(new BasicNameValuePair(name, value));
	}

	public String getNavigationUrl() {
		return navigationUrl;
	}

	public void setNavigationUrl(String navigationUrl) {
		this.navigationUrl = navigationUrl;
	}


}
